from flaskapp.util.Sqlalchemy import db

class House(db.Model):
    __tablename__ = 'house'

    house_id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.Text)
    photo_url = db.Column(db.String(255))
    monthly_rent = db.Column(db.Numeric(10, 2), nullable=False)
    rental_method = db.Column(db.Enum('整租', '合租'), nullable=False)
    house_type = db.Column(db.String(50), nullable=False)
    floor_area = db.Column(db.Numeric(10, 2), nullable=False)
    orientation = db.Column(db.Enum('东', '南', '西', '北', '东南', '东北', '西南', '西北'), nullable=False)
    decoration = db.Column(db.Enum('毛坯', '简装', '精装'), nullable=False)
    district = db.Column(db.String(50), nullable=False)
    address = db.Column(db.String(255), nullable=False)
    landlord_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    status = db.Column(db.Enum('闲置', '出租中'), default='闲置')

    def __init__(self, monthly_rent, rental_method, house_type, floor_area, orientation,
                 decoration, district, address, landlord_id, description=None, photo_url=None, status='闲置'):
        self.monthly_rent = monthly_rent
        self.rental_method = rental_method
        self.house_type = house_type
        self.floor_area = floor_area
        self.orientation = orientation
        self.decoration = decoration
        self.district = district
        self.address = address
        self.landlord_id = landlord_id
        self.description = description
        self.photo_url = photo_url
        self.status = status

    def to_dict(self):
        return {
            'house_id': self.house_id,
            'description': self.description,
            'photo_url': self.photo_url,
            'monthly_rent': float(self.monthly_rent),
            'rental_method': self.rental_method,
            'house_type': self.house_type,
            'floor_area': float(self.floor_area),
            'orientation': self.orientation,
            'decoration': self.decoration,
            'district': self.district,
            'address': self.address,
            'landlord_id': self.landlord_id,
            'status': self.status
        }